package base

import (
	"fmt"
	"sync"
	"time"
)

var m *sync.RWMutex
var val = 0

func ReadOrWriter() {
	m = new(sync.RWMutex)
	for i := 0; i < 5; i++ {
		go read(1)
	}
	for j := 0; j < 5; j++ {
		go write(2)
	}
	for m := 0; m < 5; m++ {
		go read(3)
	}

	time.Sleep(18 * time.Second)
}

func read(i int) {
	m.RLock()
	fmt.Println("读: ", i, val)
	time.Sleep(3 * time.Second)
	fmt.Println("读结束")
	m.RUnlock()
}

func write(i int) {
	m.Lock()
	val = 10
	fmt.Println("写: ", i, val)
	time.Sleep(3 * time.Second)
	fmt.Println("写结束")
	m.Unlock()
}
